package com.yuyou.weixin.util;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.yuyou.weixin.vo.resp.UserAssistStatisExcel;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Administrator on 2018/11/12.
 */
public class Webs {

    private static SimpleDateFormat sdf = new SimpleDateFormat("-yyyyMMddHHmm");

    public static void writeExcel(HttpServletRequest request, HttpServletResponse response, String fileName, List<? extends BaseRowModel > data, Class<? extends BaseRowModel> clazz)  {
        try{
            fileName = fileName + sdf.format(DateUtil.now());
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            String browserName=request.getHeader("User-Agent").toLowerCase();
            if (browserName.indexOf("firefox")>0){/*火狐浏览器*/
                response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1")+".xlsx");
            }else{
                response.setHeader("Content-Disposition","attachment;Filename=" + URLEncoder.encode( fileName ,"utf-8") + ".xlsx");
            }
            OutputStream out = response.getOutputStream();
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
            Sheet sheet1 = new Sheet(1, 0, clazz);
            writer.write(data, sheet1);
            writer.finish();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
    }
}
